package com.dyw.blog.controller;


import com.dyw.blog.common.aop.LogAnnotation;
import com.dyw.blog.common.cache.Cache;
import com.dyw.blog.service.ArticleService;
import com.dyw.blog.vo.params.ArticleParam;
import com.dyw.blog.vo.params.PageParams;
import com.dyw.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Devil
 * @create 2022-02-10 16:16
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    //加上此注解,代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5*60*1000,name="list_article")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页最热文章
     * @return
     */
    @PostMapping("/hot")
    @Cache(expire = 5*60*1000,name="hot_article")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页最新文章
     * @return
     */
    @PostMapping("/new")
    public Result newArticle(){
        int limit = 5;
        return articleService.newArticle(limit);
    }

    /**
     * 首页文章归档
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }

    @PostMapping("view/{id}")
    public Result viewArticleById(@PathVariable("id") long id){
        return articleService.findArticleById(id);
    }

    @PostMapping("/publish")
    public Result publishArticle(@RequestBody ArticleParam articleParam){
        return articleService.publishArticle(articleParam);
    }
}
